package com.abberbex;


import org.newdawn.fizzy.World;
import org.newdawn.fizzy.World.OutOfBoundsBehavior;
import org.newdawn.slick.AngelCodeFont;
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.Image;
import org.newdawn.slick.Input;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.Sound;
import org.newdawn.slick.geom.Rectangle;
import org.newdawn.slick.state.BasicGameState;
import org.newdawn.slick.state.StateBasedGame;

/**
 * 
 * @author Brendan Dickerson
 *
 */
public class LevelOneState  extends BasicGameState  {

	private final int stateID;
	private AngelCodeFont font;
	private long elapsedTime;
	private World level;
	private Player p;
	private float px = 10;		//Initial x position of player.
	private float py = 10;		//Initial y position of player.
	private Image cursor;
	private boolean touching;


	@SuppressWarnings("unused")
	private Room room;

	private Image bkg;

	private Portal portal;

	private LevelOneTerrain terrain;
	
	private Rectangle endBounds;

	private Image overlay;
	
	private Sound port;
	private Sound collide;
	
	public LevelOneState(int ID)
	{
		this.stateID=ID;
	}

	@Override
	public void init(GameContainer cont, StateBasedGame game)
			throws SlickException {
		port=new Sound("audio/quick_zap.ogg");
		collide = new Sound("audio/collide.ogg");
		

		portal = new Portal();

		level = new World(3.8f);
		level.setOutOfBoundsBehavior(OutOfBoundsBehavior.DESTROY);

		cursor = new Image("gui/cursor.png").getScaledCopy(16, 16);
		cont.setMouseCursor(cursor, cursor.getWidth()/2, cursor.getHeight()/2);
		font = new AngelCodeFont("font/font.fnt", "font/font.png");

		p = new Player(px,py);
		level.add(p);

		bkg = new Image("terrain/darkbkg.png");

		terrain = new LevelOneTerrain();
		terrain.addToWorld(level);

		room = new Room(cont, level);
		
		endBounds = new Rectangle(11*64, 0,64*2,64*2);
		
		overlay= new Image("gui/overlay.png");
	}

	@Override
	public void render(GameContainer cont, StateBasedGame game, Graphics g)
			throws SlickException {
		cont.setVSync(true);
		bkg.draw(0,0,cont.getWidth(),cont.getHeight());
		terrain.draw();
		font.drawString(cont.getWidth()/4-75, 10, "Level One");
		font.drawString(3*cont.getWidth()/4-75, 10, "Press 'p' For Help");
		portal.draw(g);
		p.draw(g);
		if(cont.getInput().isKeyDown(Input.KEY_P))
		{
			overlay.draw(0,0,cont.getWidth(),cont.getHeight());
			font.drawString(cont.getWidth()/2-76, 128, "CONTROLS");
			font.drawString(cont.getWidth()/4, 160, "   W:                 Jump");
			font.drawString(cont.getWidth()/4, 192, "   A:                  Move Left");
			font.drawString(cont.getWidth()/4, 224, "   D:                  Move Right");
			font.drawString(cont.getWidth()/4, 256, "   Right Click:   Red Portal");
			font.drawString(cont.getWidth()/4, 288, "   Left Click:     Blue Portal");
		}

	}

	@Override
	public void update(GameContainer cont, StateBasedGame game, int delta)
			throws SlickException {

		
		Input input = cont.getInput();

		p.update(input, delta);

		if(elapsedTime>1000)
		{
			elapsedTime = 0;
		}

		level.update(delta*.005f);

		if(portal.isRedOpen){
			if(p.getBounds().intersects(portal.getrBounds())&&portal.isBlueOpen)
			{	
				port.play();
				float vx = p.getXVelocity();
				float vy = p.getYVelocity();
				if((portal.bU||portal.rU)&&(portal.rU||portal.bU))
				{
					p.setPosition(portal.getbBounds().getCenterX(), portal.getbBounds().getCenterY()-26);
					p.setVelocity(vx, -vy);
				}
				if((portal.bU)&&(portal.rD))
				{
					p.setPosition(portal.getbBounds().getCenterX(), portal.getbBounds().getCenterY()+26);
					p.setVelocity(vx, vy);
				}
				if((portal.bD)&&(portal.rU))
				{
					p.setPosition(portal.getbBounds().getCenterX(), portal.getbBounds().getCenterY()-26);
					p.setVelocity(vx, vy);
				}
				if(portal.rU&&portal.bL)
				{
					p.setPosition(portal.getbBounds().getCenterX()-26, portal.getbBounds().getCenterY());
					p.setVelocity(-vy, 0);
				}
				if(portal.rU&&portal.bR){
					p.setPosition(portal.getbBounds().getCenterX()+26, portal.getbBounds().getCenterY());
					p.setVelocity(vy, 0);
				}
			}
			if(p.getBounds().intersects(endBounds))
			{
				game.enterState(0);
				p.setPosition(10, 10);
			}
		}
		if(portal.isBlueOpen){
			if(p.getBounds().intersects(portal.getbBounds())&&portal.isRedOpen)
			{	
				port.play();
				float vx = p.getXVelocity();
				float vy = p.getYVelocity();
				if((portal.bU)&&(portal.rU))
				{
					p.setPosition(portal.getrBounds().getCenterX(), portal.getrBounds().getCenterY()-26);
					p.setVelocity(vx, -vy);
				}
				if((portal.bU)&&(portal.rD))
				{
					p.setPosition(portal.getrBounds().getCenterX(), portal.getrBounds().getCenterY()+26);
					p.setVelocity(vx, vy);
				}
				if((portal.bD)&&(portal.rU))
				{
					p.setPosition(portal.getrBounds().getCenterX(), portal.getrBounds().getCenterY()-26);
					p.setVelocity(vx, vy);
				}
				if(portal.bU&&portal.rR){
					p.setPosition(portal.getrBounds().getCenterX()+26, portal.getrBounds().getCenterY());
					p.setVelocity(vy, 0);
				}
				if(portal.bU&&portal.rL){
					p.setPosition(portal.getrBounds().getCenterX()-26, portal.getrBounds().getCenterY());
					p.setVelocity(-vy, 0);
				}
				
			}
		}
	}

	public void mousePressed(int button, int x, int y)
	{
		for(int i = 0; i<terrain.getPortableWalls().length; i++)
		{
			if(terrain.getPortableWalls()[i].getBounds().contains(x, y))
			{
				if(button==0)
				{
					if(p.getX()<x)
					{
						portal.bL=true;
						portal.bR=false;
					}
					if(p.getX()>x)
					{
						portal.bR=true;
						portal.bL=false;
					}
					portal.createBlue(terrain.getPortableWalls()[i]);
				}
				if(button==1)
				{
					if(p.getX()<x)
					{
						portal.rL=true;
						portal.rR=false;
					}
					if(p.getX()>x)
					{
						portal.rR=true;
						portal.rL=false;
					}
					portal.createRed(terrain.getPortableWalls()[i]);
				}
			}
		}
		
		for(int i = 0; i<terrain.getPortableSurfaces().length; i++)
		{
			if(terrain.getPortableSurfaces()[i].getBounds().contains(x, y))
			{
				if(button==0)
				{
					portal.bL=false;
					portal.bR=false;
					if(p.getY()<y)
					{
						portal.bU=true;
						portal.bD=false;
					}
					if(p.getY()>y)
					{
						portal.bD=true;
						portal.bU=false;
					}
					portal.createBlue(terrain.getPortableSurfaces()[i]);

				}
				if(button==1)
				{
					portal.rR=false;
					portal.rL=false;
					if(p.getY()<y)
					{
						portal.rU=true;
						portal.rD=false;
					}
					if(p.getY()>y)
					{
						portal.rD=true;
						portal.rU=false;
					}
					portal.createRed(terrain.getPortableSurfaces()[i]);

				}
				//				System.out.println("port"+terrain.getPortableSurfaces()[i].getLocalX()+" , "+terrain.getPortableSurfaces()[i].getLocalY()+
				//						" ," +terrain.getPortableSurfaces()[i].getBounds().getWidth()+" , " +terrain.getPortableSurfaces()[i].getBounds().getHeight());
			}
		}
	}


	@Override
	public int getID() 
	{
		return stateID;
	}
}
